<?php
/*
Plugin Name: HidePost
Plugin URI: http://nguyenthanhcong.com/hidepost-plugin-for-wordpress/
Description: Protect a part of your post by using [protect] and [/protect]  between the protected content.
Version: 2.2
Author: Fu4ny
Author URI: http://nguyenthanhcong.com
*/

//Init the plugin
include("option_init.php");
$content_text = str_replace('%login%','<a href='.get_bloginfo('url').'/wp-login.php'.'>Login</a>',$content_text);
$content_text = str_replace('%register%','<a href='.get_bloginfo('url').'/wp-login.php?action=register'.'>Register</a>',$content_text);
$link_text = str_replace('%login%','<a href='.get_bloginfo('url').'/wp-login.php'.'>Login</a>',$link_text);
$link_text = str_replace('%register%','<a href='.get_bloginfo('url').'/wp-login.php?action=register'.'>Register</a>',$link_text);


//Link protect, simply a copy ^^
add_filter('the_content', 'link_protect');
function link_protect($content) {
global $current_user, $user_ID, $link_text, $hide_link;
if ($hide_link==0) {return $content;}
$open_tag = '<a ';$close_tag = '</a>';
$code_start = strpos($content, $open_tag); // Find the begin tag
$code_end = strpos($content, $close_tag,$code_start); // Find the end tag
if (($code_start !== false) && ($code_end !== false)) {// If has protect tag
    if (($current_user->id == 0) && ($user_ID == 0)){ // If not register or not logged in
        $newcontent = '';$rest_content = $content;
        while ((($code_start = strpos($rest_content, $open_tag)) !== false) && (($code_end = strpos($rest_content, $close_tag,$code_start)) !== false)){
        $code_end += strlen($close_tag);
        $text_start = substr($rest_content,0,$code_start); //Get the string
        $new_content .= $text_start.'<p>'.$link_text.'<br />'; //Add to content
        $rest_content = substr($rest_content,$code_end,strlen($rest_content)); //Get the rest
        }
        $new_content .= $rest_content;//Get the rest
        } else {
                $new_content = $content;
               }
    return $new_content;
} else { return $content; }
}

//Replace the content
add_filter('the_content', 'code_protect_parser');
function code_protect_parser($content) {
global $current_user, $user_ID, $content_text, $open_tag, $close_tag;
$code_start = strpos($content, $open_tag); // Find the begin tag
$code_end = strpos($content, $close_tag,$code_start); // Find the end tag
if (($code_start !== false) && ($code_end !== false)) {// If has protect tag
    if (($current_user->id == 0) && ($user_ID == 0)){ // If not register or not logged in
        $newcontent = '';$rest_content = $content;
        while ((($code_start = strpos($rest_content, $open_tag)) !== false) && (($code_end = strpos($rest_content, $close_tag,$code_start)) !== false)){
        $code_end += strlen($close_tag);
        $text_start = substr($rest_content,0,$code_start); //Get the string
        $new_content .= $text_start.'<p>'.$content_text.'<br />'; //Add to content
        $rest_content = substr($rest_content,$code_end,strlen($rest_content)); //Get the rest
        }
        $new_content .= $rest_content;//Get the rest
        } else {
                $new_content = $content;
                $new_content = str_replace($open_tag,'',$new_content); //Remove tag
                $new_content = str_replace($close_tag,'',$new_content); //Remove tag
               }
    return $new_content;
} else { return $content; }
}

//Add the Option Page
function hidepost_options()	{
        if (function_exists('add_options_page')) {
            add_options_page('HidePost Options','HidePost', 6, 'hidepost/options.php');
        }
    }
add_action('admin_menu', 'hidepost_options');
?>